package com.kye.express.business.freight_rates_query;

import android.content.Context;
import android.text.TextUtils;

import com.kye.express.R;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.RxView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.freight_rates_query.QueryAreaByCustomerCodeResultBean;
import com.kye.model.bean.freight_rates_query.QueryBillingMethodResultBean;
import com.kye.model.bean.freight_rates_query.QueryCostResultBean;
import com.kye.model.bean.freight_rates_query.QueryFreightRightsResultBean;
import com.kye.model.bean.freight_rates_query.RequestQueryAreaByCustomerCodeBean;
import com.kye.model.bean.freight_rates_query.RequestQueryBillingMethodBean;
import com.kye.model.bean.freight_rates_query.RequestQueryCostBean;
import com.kye.model.bean.freight_rates_query.RequestQueryFreightRightsBean;
import com.kye.model.bean.user.User;
import com.kye.model.service.FreightRatesQueryService;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public interface FreightRatesDialogFragmentContract {

    interface FreightRatesDialogFragmentView extends RxView {

        void onLoaded(OpenApiResult<QueryFreightRightsResultBean> queryCostResult);

        void onComplete();

        void onError(String msg);

        void onLoading();
    }

    class FreightRatesDialogFragmentViewPresenter extends BasePresenter<FreightRatesDialogFragmentContract.FreightRatesDialogFragmentView> {

        private Context mContext;

        public FreightRatesDialogFragmentViewPresenter(Context context) {
            this.mContext = context;
        }

        /**
         * 验证码验证
         */
        public void queryFreightRights(String confirmCode) {
            String phone = "";
            User user = UserSession.getInstance().getUser();
            if (user != null) {
                phone = user.getPhone();
            }

            RetrofitManager.getInstance().createOpenApiService(FreightRatesQueryService.class)
                    .queryFreightRights(new RequestQueryFreightRightsBean(phone, confirmCode))
                    .compose(getView().<OpenApiResult<QueryFreightRightsResultBean>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<QueryFreightRightsResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError(mContext.getString(R.string.common_net_error));
                        }

                        @Override
                        public void onLoaded() {
                            if (getView() != null) {
                                getView().onComplete();
                            }
                        }

                        @Override
                        public void onSuccess(OpenApiResult<QueryFreightRightsResultBean> result, QueryFreightRightsResultBean datas) {
                            getView().onLoaded(result);
                        }

                        @Override
                        public void onApiError(OpenApiResult<QueryFreightRightsResultBean> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onError(msg);
                            } else {
                                getView().onError(mContext.getString(R.string.common_net_error));
                            }
                        }

                    });
        }
    }
}
